package com.mai4j.maiadmin.controller.api.system.user;

import com.mai4j.maiadmin.misc.StructMapper;
import com.mai4j.maiadmin.model.dto.PageDTO;
import com.mai4j.maiadmin.model.dto.UserDTO;
import com.mai4j.maiadmin.model.http.datatables.Datatable;
import com.mai4j.maiadmin.model.http.datatables.response.DatatableResp;
import com.mai4j.maiadmin.service.UserService;
import com.mai4j.maiadmin.support.security.aop.annotation.RequiresAuthentication;
import com.mai4j.maiadmin.support.web.binding.ReqParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
@RequiresAuthentication
@RestController
@RequestMapping("/api/system/user/user")
public class ApiUserController {

    private final UserService userService;

    public ApiUserController(UserService userService) {
        this.userService = userService;
    }

    /*
    @PostMapping("/saveOrUpdate")
    public ResponseEntity<SimpleResp> saveOrUpdate(@RequestBody @Validated LanguageSaveOrUpdateReq languageSaveOrUpdateReq) {
        languageService.saveOrUpdate(StructMapper.INSTANCE.languageSaveOrUpdateReqToLanguageDTO(languageSaveOrUpdateReq));
        return ResponseEntity.ok(new SimpleResp()
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    @PostMapping("/delete")
    public ResponseEntity<SimpleResp> delete(@RequestBody @Validated LanguageDeleteReq languageDeleteReq) {
        languageService.delete(Long.parseLong(languageDeleteReq.getLanguageId()));
        return ResponseEntity.ok(new SimpleResp()
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }*/

    /**
     * 分页
     *
     * @param datatable datatable
     */
    @PostMapping("/datatable")
    public ResponseEntity<DatatableResp> datatable(@ReqParam Datatable datatable) {
        PageDTO<UserDTO> pageDTO = userService.paginate(datatable);
        return ResponseEntity.ok(new DatatableResp()
                .setDraw(datatable.getDraw())
                .setRecordsFiltered(pageDTO.getTotalRow())
                .setRecordsTotal(pageDTO.getTotalRow())
                .setData(StructMapper.INSTANCE.userDTOToUserVO(pageDTO.getList())));
    }
}
